package
{
	import flash.events.Event;

	public class XMLStorage
	{
		
		
		
		public function XMLStorage()
		{
			//stage.nativeWindow.addEventListener(Event.CLOSING, windowClosingHandler); 
			facebookAccountFile = File.applicationStorageDirectory;
			facebookAccountFile = facebookAccountFile.resolvePath("facebookinfo.xml");
			readXML();
		}
		
		import flash.filesystem.*;
		
		public var facebookAccountFile:File; // The facebookinfo prefsFile
		[Bindable] public var facebookInfoXML:XML; // The XML data
		public var stream:FileStream; // The FileStream object used to read and write prefsFile data.
		
		public var facebookInfo:FacebookInfo;
		
		
		
		private function readXML():void 
		{
			stream = new FileStream();
			if (facebookAccountFile.exists) {
				stream.open(facebookAccountFile, FileMode.READ);
				processXMLData();
			}
			else
			{
				saveData();
			}
			//stage.nativeWindow.visible = true;
		}
		
		private function processXMLData():void 
		{
			facebookInfoXML = XML(stream.readUTFBytes(stream.bytesAvailable));
			stream.close();
			//userInfo.facebook_session = prefsXML.userInfo.@facebookSession;
			
			trace("xml storage are loaded");
			trace("prefsXML is : " + facebookInfoXML.toString());
			
			facebookInfo = transXMLtoFacebookInfoClass(facebookInfoXML);
			
		}
		
		
		public function getFacebookInfo():FacebookInfo{
			
			trace("getting user information from XMLStorage..");
			return facebookInfo;
		}
		
		private function transXMLtoFacebookInfoClass(facebookInfoXML:XML):FacebookInfo{
			var facebookInfo:FacebookInfo = new FacebookInfo;
			
			var tempObj:Object = new Object();
			
			facebookInfo.facebook_uid = facebookInfoXML.session.facebook_uid;
			facebookInfo.facebook_name = facebookInfoXML.session.facebook_name;
			facebookInfo.username = facebookInfoXML.session.username;
			facebookInfo.accessToken = facebookInfoXML.session.accessToken;
			facebookInfo.birthDay = facebookInfoXML.session.birthDay;
			facebookInfo.email = facebookInfoXML.session.email;
			facebookInfo.gender = facebookInfoXML.session.gender;
			
			tempObj.id = facebookInfoXML.session.hometown.@id;
			tempObj.name = facebookInfoXML.session.hometown.@name;
			facebookInfo.hometown = tempObj;
			
			tempObj.id = facebookInfoXML.session.location.@id;
			tempObj.name = facebookInfoXML.session.location.@name;
			facebookInfo.location = tempObj;
			
			return facebookInfo;
		}
		
		private function windowClosingHandler(event:Event):void {
			//saveData();
		}
		
		
		private function saveData():void
		{
			createXMLData(); 
			writeXMLData();
		}
		
		
		private function createXMLData():void 
		{
			facebookInfoXML = <facebookinfo/>;
			
			
			facebookInfoXML.session.facebook_uid = '';
			facebookInfoXML.session.facebook_name = '';
			facebookInfoXML.session.username = '';
			facebookInfoXML.session.accessToken = '';
			facebookInfoXML.session.birthDay = '';
			facebookInfoXML.session.email = '';
			facebookInfoXML.session.gender = '';
			facebookInfoXML.session.hometown.@id = '';
			facebookInfoXML.session.hometown.@name = '';
			facebookInfoXML.session.location.@id = '';
			facebookInfoXML.session.location.@name = '';
			
			
			
			
			/*
			prefsXML.windowState.@height = stage.nativeWindow.height;
			prefsXML.windowState.@x = stage.nativeWindow.x;
			prefsXML.windowState.@y = stage.nativeWindow.y;
			*/
			facebookInfoXML.saveDate = new Date().toString();
		}
		
		
		private function writeXMLData():void 
		{
			var outputString:String = '<?xml version="1.0" encoding="utf-8"?>\n';
			outputString += facebookInfoXML.toXMLString();
			outputString = outputString.replace(/\n/g, File.lineEnding);
			stream = new FileStream();
			stream.open(facebookAccountFile, FileMode.WRITE);
			stream.writeUTFBytes(outputString);
			stream.close();
			
			trace("writed Strings : " + outputString);
			trace("writed xml");
		}
		
		
		public function saveByFacebookInfo(facebookInfo:FacebookInfo):void{
			facebookInfoXML.session.facebook_uid = facebookInfo.facebook_uid;
			facebookInfoXML.session.facebook_name = facebookInfo.facebook_name;
			facebookInfoXML.session.username = facebookInfo.username;
			facebookInfoXML.session.accessToken = facebookInfo.accessToken;
			facebookInfoXML.session.birthDay = facebookInfo.birthDay;
			facebookInfoXML.session.email = facebookInfo.email;
			facebookInfoXML.session.gender = facebookInfo.gender;
			facebookInfoXML.session.hometown.@id = facebookInfo.hometown.id;
			facebookInfoXML.session.hometown.@name = facebookInfo.hometown.name;
			facebookInfoXML.session.location.@id = facebookInfo.location.id;
			facebookInfoXML.session.location.@name = facebookInfo.location.name;
			
			trace("user info was saved to storage.");
			
			writeXMLData();
			
			
		}
		
	}
}